//
//  UserTypeViewController.m
//  HongFuShop
//
//  Created by chirui on 2017/8/21.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "UserTypeViewController.h"
#import "UserTypeMessaheViewController.h"

@interface UserTypeViewController ()<UITableViewDelegate,UITableViewDataSource>{

    UITableView * _tableView;
    NSString * chooseStr;
    NSString * chooseId;
    NSInteger  integer;
    NSArray * dataArray;

}

@end

@implementation UserTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    
    chooseStr = @"";
    chooseId = @"";
    integer = -1;
    
    self.view.backgroundColor =[UIColor groupTableViewBackgroundColor];
    self.navigationItem.titleView=[TopLabel withLabelWithLabelText:@"用户等级"];
    
    [self creatTableView];
    [self getSellerRequest];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_public_return" target:self action:@selector(back)];
    
    
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
    
//     self.block(chooseStr,chooseId);
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64-48) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = NO;
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 70;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 120;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * cellId = @"cellId";
    
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:cellId ];
    if (cell==nil) {
        
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }
    
    cell.contentView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    
    
    NSDictionary * dic = dataArray[indexPath.row];
    
    UIImageView * view =[[UIImageView alloc]initWithFrame:CGRectMake(5, 20, WIDTH-10, 50)];
    view.backgroundColor =[UIColor whiteColor];
    view.userInteractionEnabled = YES;
    [cell.contentView addSubview:view];
    
    if (integer==indexPath.row) {
        
        view.image = ImageName(@"icon__list_frame");
    }else {
    
        view.image = ImageName(@"");
    }
    
    UILabel * messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 10, WIDTH - 20-80, 30)];
    messageLabel.text = [NSString stringWithFormat:@"%@     目标积分：%@",dic[@"level_name"],dic[@"target_score"]];
    messageLabel.font =[UIFont systemFontOfSize:15];
    [view addSubview:messageLabel];
    
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(WIDTH -90, 10, 80, 30);
    [btn setTitle:@"查看协议" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = 100+indexPath.row;
    [view addSubview:btn];
    
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
 
    
    
    return cell;
}

-(void)btnClick:(UIButton *)btn {

    NSDictionary * dic = dataArray[btn.tag -100];
    
    UserTypeMessaheViewController * user = [[UserTypeMessaheViewController alloc]init];
    user.urlStr = dic[@"url"];
    [self.navigationController pushViewController:user animated:YES];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    integer = indexPath.row;
//    NSDictionary * dic = dataArray[indexPath.row];
//    
//    chooseStr = dic[@"level_name"];
//    chooseId = dic[@"level_id"];
    
    [_tableView reloadData];
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 120)];
    view.backgroundColor =[UIColor clearColor];
    
    UIButton * btn =[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(50, 60, WIDTH- 100, 40);
    btn.layer.cornerRadius = 20.f;
    btn.layer.masksToBounds = YES;
    [btn setBackgroundImage:ImageName(@"img_home_nav_bg") forState:UIControlStateNormal];
    [btn setTitle:@"申请" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(appliFor:) forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:btn];

    return view;
    
}

-(void)appliFor:(UIButton *)btn{

    if (integer<0) {
        SHuiMBhudTextTimeStr(@"请选择等级！");
        
        return;
    }
    
    NSDictionary * dic = dataArray[integer];
    
     [self.navigationController popViewControllerAnimated:YES];
    
     self.block(dic[@"level_name"],dic[@"level_id"]);
    
//    [self getuserLevelRequestFor:dic[@"level_id"]];
    
}

//获取等级列表
-(void)getSellerRequest{
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_userLevel_api) andparameters:nil Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"code"] intValue]== 200) {
            
            dataArray = responseObject[@"datas"];
            
        }else{
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
        [_tableView reloadData];
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        
        SHuiMBhudTextTimeStr(@"网络错误，请稍后再试");
    }];
}


//获取等级列表
-(void)getuserLevelRequestFor:(NSString *)level{
    
    NSDictionary * dic = @{@"user_id":USER_DEFAULTGet(@"user_id"),
                           @"token":USER_DEFAULTGet(@"token"),
                           @"level_id":level};

    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(set_userLevel_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"code"] intValue]== 200) {
            
            SHuiMBhudTextTimeStr(@"申请已发出等待审核");
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }else{
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
        [_tableView reloadData];
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        
        SHuiMBhudTextTimeStr(@"网络错误，请稍后再试");
    }];
}



@end
